package ar.edu.uba.fi.jmit.integrationcommons;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.integrationcommons.EventPriority;
import ar.edu.uba.fi.jmit.integrationcommons.Header;
import ar.edu.uba.fi.jmit.integrationcommons.Message;
import ar.edu.uba.fi.jmit.integrationcommons.MessagePriority;

public abstract class IntegrationTestCase extends TestCase {
	
	protected static Logger logger;
	
	public IntegrationTestCase() {
		Properties props = new Properties();
		try {
			props.load(this.getClass().getClassLoader().getResourceAsStream("log4j-integration-commons-test.properties"));
		} catch (IOException exception) {
			exception.printStackTrace();
		}
		PropertyConfigurator.configure(props);
		logger = Logger.getLogger(this.getClass());
	}
	
	protected void fail(Exception e) {
		logger.error(e);
		e.printStackTrace();
		fail(e.getMessage());
	}
	
	protected Message createMessage(int id) {
		Message message = new Message();
		message.setHeader(new Header());
		message.setBody(createEvent());
		message.getHeader().setId(String.valueOf(id));
		message.getHeader().setPriority(MessagePriority.NORMAL);
		message.getHeader().setCreationDate(new Date());
		message.getHeader().setSourceApp("senderTestCase");
		message.getHeader().setDestinationName("senderTestCase");
		return message;
	}
	
	protected Event createEvent() {
		Event event = new Event();
		event.setPriority(EventPriority.NORMAL);
		event.setSource("EndpointImplTest.createEvent()");
		event.addData("param1", "value1");
		return event;
	}

}
